package jaux.tank.generator.component;

import java.awt.Dimension;
import java.awt.Image;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

public class TankGeneratorToolBar extends JToolBar{

	private static final long serialVersionUID = 1L;

	private static final int DEFAULT_TOOLBAR_BUTTON_SIZE = 36;
	TankGeneratorFrame tankFrame;
	
	public TankGeneratorToolBar(TankGeneratorFrame tankFrame) {
		super();
		this.tankFrame = tankFrame;
		init();
	}
	private void init() {

			JButton newButton = createToolbarButton("img/New.png", tankFrame.getString("toolbar.new"));
			newButton.addActionListener(e->tankFrame.newTankRoot());
			add(newButton);

			JButton openButton = createToolbarButton("img/Open.png", tankFrame.getString("toolbar.open"));
			openButton.addActionListener(e -> tankFrame.openTank());
			add(openButton);

			JButton saveButton = createToolbarButton("img/Save.png", tankFrame.getString("toolbar.save"));
			saveButton.addActionListener(e -> tankFrame.saveTank());
			add(saveButton);

			JButton saveAsButton = createToolbarButton("img/SaveAs.png", tankFrame.getString("toolbar.save_as"));
			saveAsButton.addActionListener(e -> tankFrame.saveAsTank());
			add(saveAsButton);
			
			JButton closeButton = createToolbarButton("img/Close.png", tankFrame.getString("toolbar.close"));
			closeButton.addActionListener(e -> tankFrame.closeTank());
			add(closeButton);

			JButton scriptButton = createToolbarButton("img/Script.png", tankFrame.getString("toolbar.script"));
			scriptButton.addActionListener(e -> tankFrame.openScriptDialog());
			add(scriptButton);
			
//			JButton undoButton = createToolbarButton("img/Undo.png", tankFrame.getString("toolbar.undo"));
//			undoButton.setEnabled(false);
//			add(undoButton);
//			
//			JButton redoButton = createToolbarButton("img/Redo.png", tankFrame.getString("toolbar.redo"));
//			redoButton.setEnabled(false);
//			add(redoButton);

			JButton validateButton = createToolbarButton("img/Check.png", tankFrame.getString("toolbar.validate"));
			validateButton.setEnabled(false);
			add(validateButton);
			
			JButton applyButton = createToolbarButton("img/Publish.png", tankFrame.getString("toolbar.apply"));
			applyButton.setEnabled(false);
			add(applyButton);
	}

	private JButton createToolbarButton(String iconPath, String tooltip) {
		
		URL iconURL = ClassLoader.getSystemResource(iconPath);
		JButton button = new JButton();
		button.setPreferredSize(new Dimension(DEFAULT_TOOLBAR_BUTTON_SIZE, DEFAULT_TOOLBAR_BUTTON_SIZE));
		button.setToolTipText(tooltip);
		if(iconURL == null) {
			button.setText(tooltip);
		}else {
			// gen a icon fit for toolbar button
			ImageIcon icon = new ImageIcon(iconURL);
			Image image = icon.getImage();
			Image newimg = image.getScaledInstance(DEFAULT_TOOLBAR_BUTTON_SIZE-10, DEFAULT_TOOLBAR_BUTTON_SIZE-10, java.awt.Image.SCALE_SMOOTH);
			icon = new ImageIcon(newimg);
			button.setIcon(icon);
		}
		return button;
	}
}
